<?php
namespace App\Http\Controllers\Admin;
use DB;
use App\User;
use Validator;
use App\Http\Controllers\Controller;
use Illuminate\Foundation\Admin\ThrottlesLogins;
use Illuminate\Foundation\Admin\AuthenticatesAndRegistersUsers;
use Illuminate\Http\Request;
use Storage;
use App\Http\Requests;

class ProductController extends Controller
{
	public $product=false;
	public function __construct(){
         $this->product = new \App\Product();
         $this->goodsattr = new \App\Goodsattr();
         $this->attr = new \App\Attr();
    }
    public function product_add(){
        $param = $_POST;
        $data['product_attr'] = implode('|', $param['product_attr']);
        // var_dump($data);die;
        $data['goods_id'] = $param['goods_id'];
        $data['product_number'] = $param['product_number'];
        $productOne = $this->product->getOne($data['goods_id'],$data['product_attr']);
        //var_dump($productOne);die;
        if(empty($productOne[0])){
        	// echo 1;
        	 //添加货品
            $result = $this->product->addOne($data);
            if(!$result){
            	echo 0;
            	 // echo "<script>alert('添加货品组合失败！');location.href='product_list';</script>";
            }else{
            	echo 1;
            	//echo "<script>alert('添加货品组合成功！');location.href='goods_list';</script>";
            }
        	
        }
	}
	public function product_list($goods_id){
		$product = $this->getProduct($goods_id);
        $specList = $this->getGoodsSpec($goods_id);
		return view('Admin/Product/product_list',['product'=>$product,'specList'=>$specList,'goods_id'=>$goods_id]);
	}
	/**
     * 获取货品数据
     */
	public function getProduct($goods_id){
		$productList = $this->product->getAll($goods_id);
		$attrList = $this->goodsattr->getList($goods_id);
		$goodsAttr = array_combine(array_column($attrList, 'goods_attr_id'), $attrList);
		foreach($productList as &$product){
            $product['product_attr'] = explode('|', $product['product_attr']);
            foreach($product['product_attr'] as &$goodsAttrID){
                $goodsAttrID = $goodsAttr[$goodsAttrID]['attr_value'];
            }
        }
       
        return $productList;
	}
	 /**
     * 获取商品可选的 规格列表
     */
	public function getGoodsSpec($goods_id){
		//获取商品规格属性
        $goodsSpecList = $this->goodsattr->getLists($goods_id);
        $attrID = array_column($goodsSpecList, 'attr_id'); //取出数组中指定的一列 获取属性id
        //把属性数据的 主键 变成 下标   组合，可以把数组当中的一列 变成 该数组的 下标
        $arrtQuery = $this->get_where_string($this->my_unique($attrID), 'attr_id'); //属性id去重 转换成查询条件字符串
        foreach ($goodsSpecList as $spec) {
            $arrtQuery[$spec['attr_id']]['attr_list'][] = $spec;
        }
        return $arrtQuery;
	}
	/**
 	* 生成 字符串 条件（代替in操作）
 	*/
	function get_where_string($arr, $key){
		$res = [];
		$query = DB::table('attr');
		if (is_array($arr) && !empty($arr)) 
		{
			foreach ($arr as $k => $value) {
				$singleArr[$key]=$value;
				$query = $query->orWhere($singleArr);
			}
		}
		$arrs =$query->get();
		$result = [];
		foreach ($arrs as $key => $value) {
			$result[$value->attr_id]= [
				'attr_id' => $value->attr_id,
				'attr_name' => $value->attr_name,
				''
			];
		}
		return $result;
	}
	/**
	 * 数组去重
	 */
	function my_unique($arr){
	    // array_unique 数组去重 php 自带
	    // 互换键值 2次 去重
	    return array_flip(array_flip($arr));
	}


}